#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Basic run script"""

from tornado.options import options, define, parse_command_line
import django.core.handlers.wsgi
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.wsgi
import os
if django.VERSION[1] > 5:
    django.setup()

from mysite.cheungssh.ssh.handlers import WSHandler
from mysite.cheungssh.ssh.ioloop import IOLoop

define('port', type=int, default=8000)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

class HelloHandler(tornado.web.RequestHandler):
  def get(self):
    self.write('Hello from tornado')

def main():
  parse_command_line()
  wsgi_app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler())
  tornado_app = tornado.web.Application(
    [
      ('/hello-tornado', HelloHandler),
      (r"/cheungssh/telminal", WSHandler),
      ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),
      ])
  server = tornado.httpserver.HTTPServer(tornado_app)
  server.listen(options.port)
  # start ssh and WebSocket IOLoop
  IOLoop.instance().start()
  tornado.ioloop.IOLoop.instance().start()

if __name__ == '__main__':
  for i in os.sys.path: print i
  main()
